from django.shortcuts import render

# Create your views here.
from rest_framework import status
from rest_framework.generics import GenericAPIView, ListAPIView, RetrieveAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
from rest_framework.viewsets import ViewSet, GenericViewSet, ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from areas.models import Areas
from areas.serializers import AreasSerializer, SubAreasSerializer


# 升级6
class AreasViewSet(CacheResponseMixin,ReadOnlyModelViewSet):
    def get_serializer_class(self):
        """根据不同的操作返回不同的序列化器类"""
        if self.action == 'list':
            return AreasSerializer
        else:
            return SubAreasSerializer

    def get_queryset(self):
        if self.action == 'list':
            return Areas.objects.filter(parent=None)
        else:
            return Areas.objects.all()

    # 关闭分页
    pagination_class = None

# 升级5
# class AreasViewSet(ListModelMixin, GenericViewSet, RetrieveModelMixin):
#     def get_serializer_class(self):
#         if self.action == "list":
#             return AreasSerializer
#         else:
#             return SubAreasSerializer
#
#     def get_object(self):
#         if self.action == 'list':
#             return Areas.objects.filter(parent=None)
#         else:
#             return Areas.objects.all()



# 升级4
# class AreasViewSet(GenericViewSet):
#
#     def get_serializer_class(self):
#         if self.action == "list":
#             return AreasSerializer
#         else:
#             return SubAreasSerializer
#     def get_object(self):
#         if self.action == 'list':
#             return Areas.objects.filter(parent=None)
#         else:
#             return Areas.objects.all()
#
#     # GET /areas/ ->list
#     def list(self, request):
#         """
#         获取所有省的数据
#         1、从数据库中查询所有的省的信息
#         2、将省的信息序列化并返回
#         """
#         # 1、从数据库中查询所有的省的信息
#         areas = Areas.objects.filter(parent=None)
#         # 2、将省的信息序列化并返回
#         serializer = self.get_serializer(areas, many=True)
#         return Response(serializer.data)
#
#     # GET /areas/(?P<pk>\d+)/ ->retrieve
#     def retrieve(self, request, pk):
#         """
#         获取指定地区的数据（在返回指定地区的数据时，将其下级地区的数据嵌套返回）
#         1、根据pk值获取指定地区的数据
#         2、将指定地区的数据序列化并返回
#         """
#         # 1、根据pk值获取指定地区的数据
#         try:
#             area = Areas.objects.get(pk=pk)
#         except Areas.DoesNotExist:
#             return Response({'message': 'NOT Found'}, status=status.HTTP_404_NOT_FOUND)
#         # 2、将指定地区的数据序列化并返回
#         serializer = self.get_serializer(area)
#         return Response(serializer.data)

# 升级3
# class AreasViewSet(ViewSet):
#     # GET /areas/ ->list
#     def list(self, request):
#         """
#         获取所有省的数据
#         1、从数据库中查询所有的省的信息
#         2、将省的信息序列化并返回
#         """
#         # 1、从数据库中查询所有的省的信息
#         areas = Areas.objects.filter(parent=None)
#         # 2、将省的信息序列化并返回
#         serializer = AreasSerializer(areas, many=True)
#         return Response(serializer.data)
#
#     # GET /areas/(?P<pk>\d+)/ ->retrieve
#     def retrieve(self, request, pk):
#         """
#         获取指定地区的数据（在返回指定地区的数据时，将其下级地区的数据嵌套返回）
#         1、根据pk值获取指定地区的数据
#         2、将指定地区的数据序列化并返回
#         """
#         # 1、根据pk值获取指定地区的数据
#         try:
#             area = Areas.objects.get(pk=pk)
#         except Areas.DoesNotExist:
#             return Response({'message': 'NOT Found'}, status=status.HTTP_404_NOT_FOUND)
#         # 2、将指定地区的数据序列化并返回
#         serializer = SubAreasSerializer(area)
#         return Response(serializer.data)


# class AreasView(GenericAPIView):
#     queryset = Areas.object.filter(parent=None)
#     serializer_class = AreasSerializer
#
#     def get(self, request):
#         """:
#         获取所有省的数据
#         1、从数据库中查询所有省的数据
#         2、将省的数据序列化并返回
#         """
#         areas = self.get_queryset()
#         serializer = self.get_serializer(areas, many=True)
#         return Response(serializer.data)
# 优化
# class AreasView(GenericAPIView, ListModelMixin):
#     queryset = Areas.object.filter(parent=None)
#     serializer_class = AreasSerializer
#
#     def get(self, request):
#         return self.list(request)
# 再升级
# 查看省份
# 升级2
# class AreasView(ListAPIView):
#     queryset = Areas.objects.filter(parent=None)
#     serializer_class = AreasSerializer


# 获取指定地区的数据  GET /areas/(?P<pk>\d+)
# class SubAreasView(APIView):
#     def get(self, request, pk):
#         """
#         获取指定地区的下级子地区（在返回指定地区时，同时将地区关联的下级嵌套返回）
#         1、根据pk值获取其指定地区的数据
#         2、获取指定地区下面的子区数据
#         3、序列化返回数据
#         """
#         # 1、根据pk值获取其指定地区的数据
#         try:
#             areas = Areas.objects.get(pk=pk)
#         except Areas.DoesNotExist:
#             return Response({'message': '传入地区参数有误'},status=status.HTTP_404_NOT_FOUND)
#     # 2、获取指定地区下面的子区数据  对关联对象序列化器
#         serializer = SubAreasSerializer(areas)
#     # 3、序列化返回数据
#         return Response(serializer.data)

# 优化
# 获取指定地区的数据  GET /areas/(?P<pk>\d+)
# class SubAreasView(GenericAPIView):
#     serializer_class = SubAreasSerializer
#     queryset = Areas.objects.all()
#     def get(self, request, pk):
#         """
#         获取指定地区的下级子地区（在返回指定地区时，同时将其下级嵌套返回）
#         1、根据pk值获取其指定地区的数据
#         2、获取指定地区下面的子区数据
#         3、序列化返回数据
#         """
#         # 1、根据pk值获取其指定地区的数据
#         areas = self.get_object()
#         #  2、获取指定地区下面的子区数据  对关联对象序列化输出
#         serializer = self.get_serializer(areas)
#     # 3、序列化返回数据
#         return Response(serializer.data)
# 优化
# 获取指定地区的数据  GET /areas/(?P<pk>\d+)
# 升级2
# class SubAreasView(RetrieveAPIView):
#     serializer_class = SubAreasSerializer
#     queryset = Areas.objects.all()
